package com.example.music.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.music.entity.Song;
import com.example.music.service.SongService;
import com.example.music.utils.AjaxJson;
import com.example.music.utils.ContentValue;
import com.example.music.utils.FileMd5;
import com.example.music.utils.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.File;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("song")
public class SongController {
    @Autowired
    SongService songService;

    @PostMapping("add")
    public AjaxJson add(Song song,MultipartFile file) {
        return songService.addSong(song,file);
//        return AjaxJson.getSuccess();
    }

    @PostMapping("search_singerId")
    public AjaxJson searchSongsBySingerId(@RequestBody String singerId) {
        List<Song> singer_id = songService.selectItemsByParam("singer_id", singerId);
        for (Song song : singer_id) {
            String s = song.getLyric().replaceAll("\\\\n", "\n");
            song.setLyric(s);
        }
        return AjaxJson.getSuccessData(singer_id);
    }

    @PostMapping("update")
    public AjaxJson update(@RequestBody Song song) {
        Song selectSong = songService.selectItemById(song.getId().toString());
        if (selectSong == null) {
            return AjaxJson.getError("歌曲不存在");
        }
        UpdateWrapper<Song> updateWrapper = new UpdateWrapper<>();
        if (!StrUtil.isEmptyOrUndefined(song.getName())) {
            updateWrapper.set("name", song.getName());
        }
        if (!StrUtil.isEmptyOrUndefined(song.getIntroduction())) {
            updateWrapper.set("introduction", song.getIntroduction());
        }
        if (!StrUtil.isEmptyOrUndefined(song.getPic())) {
            updateWrapper.set("pic", song.getPic());
        }
        if (!StrUtil.isEmptyOrUndefined(song.getLyric())) {
            updateWrapper.set("lyric", song.getLyric());
        }
        if (!StrUtil.isEmptyOrUndefined(song.getUrl())) {
            updateWrapper.set("url", song.getUrl());
        }
        updateWrapper.eq("id",song.getId());
        updateWrapper.set("update_time", DateUtil.date());
        songService.update(song,updateWrapper);
        return AjaxJson.getSuccess();
    }

    @GetMapping("delete")
    public AjaxJson deleteById(@RequestParam String songId){
//        Song song = songService.selectItemById(songId);
//        File file = new File(ContentValue.userDir + song.getUrl());
//        file.delete();
//        System.out.println(ContentValue.userDir+song.getUrl());
        songService.deleteById(songId);
        return AjaxJson.getSuccess();
    }

    @GetMapping("song_id")
    public AjaxJson getSongById(@RequestParam String songId){
        return AjaxJson.getSuccessData(songService.selectItemById(songId));
    }

    @GetMapping("songOfSongName")
    public AjaxJson getsongOfSongName(@RequestParam String songOfSongName){
        return songService.songOfSongName(songOfSongName);
    }

    @GetMapping("songOfSingerName")
    public AjaxJson songOfSingerName(@RequestParam String songOfSingerName){
        return songService.songOfSongName(songOfSingerName);
    }

//    @GetMapping("allSong")
//    public AjaxJson getAllSongs(){
//        return AjaxJson.getSuccess(songService.selectAll().toString());
//    }
    @GetMapping("allSong")
    public AjaxJson getAllSongs(){
        return AjaxJson.getSuccessData(songService.selectAll());
    }

    @PostMapping("updateSongPic")
    public AjaxJson updateSongPic(@RequestParam Integer id, @RequestParam("file") MultipartFile pic) {
        return songService.updateSingerPic(id,pic);
    }

    @PostMapping("updateSongUrl")
    public AjaxJson updateSongUrl(@RequestParam Integer id, @RequestParam("file") MultipartFile pic) {
        return songService.updateSingerUrl(id,pic);
    }

    @GetMapping("songBySingerId")
    public AjaxJson songBySingerId(@RequestParam String singerId){
        List<Song> singer_id = songService.selectItemsByParam("singer_id", singerId);
        return AjaxJson.getSuccessData(singer_id);
    }

    @PostMapping("fileMd5")
    public void fileMd5(){
        List<Song> songs = songService.selectAll();
        String filePath = ContentValue.userDir;
        for (Song song : songs) {
            File file = new File(filePath+ContentValue.Slash+song.getUrl());
            String fileMd5 = FileMd5.getFileMd5(file);
            song.setMd5(fileMd5);
            UpdateWrapper<Song> songUpdateWrapper = new UpdateWrapper<>();
            songUpdateWrapper.eq("id", song.getId());
            songUpdateWrapper.set("md5",song.getMd5());
            songService.update(null,songUpdateWrapper);
        }
    }
}
